<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace app\commands;

use app\models\Node;
use app\models\User;
use Faker\Provider\zh_CN\Company;
use Faker\Provider\zh_CN\Person;
use Faker\Provider\zh_CN\PhoneNumber;
use yii\console\Controller;
use yii\console\ExitCode;
use Yii;

/**
 * This command echoes the first argument that you have entered.
 *
 * This command is provided as an example for you to learn how to create console commands.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class DataController extends Controller
{
    /**
     * This command echoes what you have entered as the message.
     * @param string $message the message to be echoed.
     * @return int Exit code
     */
    public function actionIndex()
    {
        echo "请使用 yii data/init 初始化数据 \n";

        return ExitCode::OK;
    }

    public function actionInit()
    {
        $auth = Yii::$app->authManager;
        $db = Yii::$app->db;
        $connection = $db->createCommand();

        $connection->setRawSql("SET FOREIGN_KEY_CHECKS=0")->execute();
        $connection->truncateTable('auth_item_child')->execute();
        $connection->truncateTable('auth_assignment')->execute();
        $connection->truncateTable('auth_item')->execute();
        $connection->truncateTable('user')->execute();
        $connection->truncateTable('node')->execute();
        $connection->truncateTable('project')->execute();
        $connection->setRawSql("SET FOREIGN_KEY_CHECKS=1")->execute();

        $transaction = $db->beginTransaction();
        try{

            echo "============用户填充【开始】============\n";
            $user = new User();
            $user->username = 'root';
            $user->password_hash = '123456';
            $user->email = 'root@qq.com';
            $user->save(false);
            echo "填充用户名：{$user->username}\n";
            echo "============用户填充【完成】============\n";
            echo "\n";

            echo "============角色填充【开始】============\n";
            $datas = [
                ['name'=>'root','description'=>'超级管理员'],
                ['name'=>'admin','description'=>'管理员'],
                ['name'=>'editor','description'=>'编辑'],
                ['name'=>'businesser','description'=>'商务'],
                ['name'=>'business-manager','description'=>'商务经理'],
            ];
            foreach ($datas as $data){
                $role = $auth->createRole($data['name']);
                $role->description = $data['description'];
                $auth->add($role);
                echo "填充角色{$data['name']}-{$data['description']}\n";
            }
            echo "============角色填充【完成】============\n";
            echo "\n";

            echo "============权限填充【开始】============\n";
            $datas = [
                [
                    'name' => 'user/*',
                    'description'=>'用户管理',
                    'child' => [
                        ['name'=>'user/index','description'=>'用户列表'],
                        ['name'=>'user/create','description'=>'添加用户'],
                        ['name'=>'user/edit','description'=>'编辑用户'],
                        ['name'=>'user/destroy','description'=>'删除用户'],
                        ['name'=>'user/role','description'=>'分配角色'],
                        ['name'=>'user/exten','description'=>'分配分机'],
                    ],
                ],
                [
                    'name' => 'role/*',
                    'description'=>'角色管理',
                    'child' => [
                        ['name'=>'role/index','description'=>'角色列表'],
                        ['name'=>'role/create','description'=>'添加角色'],
                        ['name'=>'role/edit','description'=>'编辑角色'],
                        ['name'=>'role/destroy','description'=>'删除角色'],
                        ['name'=>'role/role','description'=>'分配子角色'],
                        ['name'=>'role/permission','description'=>'分配权限'],
                    ],
                ],
                [
                    'name' => 'permission/*',
                    'description'=>'权限管理',
                    'child' => [
                        ['name'=>'permission/index','description'=>'权限列表'],
                        ['name'=>'permission/create','description'=>'添加权限'],
                        ['name'=>'permission/edit','description'=>'编辑权限'],
                        ['name'=>'permission/destroy','description'=>'删除权限'],
                    ],
                ],
                [
                    'name' => 'extension/*',
                    'description'=>'分机管理',
                    'child' => [
                        ['name'=>'extension/index','description'=>'分机列表'],
                        ['name'=>'extension/create','description'=>'添加分机'],
                        ['name'=>'extension/edit','description'=>'编辑分机'],
                        ['name'=>'extension/destroy','description'=>'删除分机'],
                    ],
                ],
                [
                    'name' => 'trunk/*',
                    'description'=>'中继管理',
                    'child' => [
                        ['name'=>'trunk/index','description'=>'中继列表'],
                        ['name'=>'trunk/create','description'=>'添加中继'],
                        ['name'=>'trunk/edit','description'=>'编辑中继'],
                        ['name'=>'trunk/destroy','description'=>'删除中继'],
                    ],
                ],
                [
                    'name' => 'routing/*',
                    'description'=>'呼出路由管理',
                    'child' => [
                        ['name'=>'routing/index','description'=>'呼出路由列表'],
                        ['name'=>'routing/create','description'=>'添加呼出路由'],
                        ['name'=>'routing/edit','description'=>'编辑呼出路由'],
                        ['name'=>'routing/destroy','description'=>'删除呼出路由'],
                    ],
                ],
                [
                    'name' => 'queue/*',
                    'description'=>'队列管理',
                    'child' => [
                        ['name'=>'queue/index','description'=>'队列列表'],
                        ['name'=>'queue/create','description'=>'添加队列'],
                        ['name'=>'queue/edit','description'=>'编辑队列'],
                        ['name'=>'queue/destroy','description'=>'删除队列'],
                    ],
                ],
                [
                    'name' => 'campaign/*',
                    'description'=>'批量呼出管理',
                    'child' => [
                        ['name'=>'campaign/index','description'=>'呼出任务列表'],
                        ['name'=>'campaign/create','description'=>'添加呼出任务'],
                        ['name'=>'campaign/status','description'=>'激活/禁用呼出任务'],
                        ['name'=>'campaign/destroy','description'=>'删除呼出任务'],
                        ['name'=>'campaign/import','description'=>'导入号码'],
                        ['name'=>'campaign/edit','description'=>'更新并发'],
                        ['name'=>'campaign/config','description'=>'服务配置'],
                    ],
                ],
                [
                    'name' => 'form/*',
                    'description'=>'客户表单管理',
                    'child' => [
                        ['name'=>'form/index','description'=>'客户表单列表'],
                        ['name'=>'form/create','description'=>'添加客户表单'],
                        ['name'=>'form/status','description'=>'激活/禁用客户表单'],
                        ['name'=>'form/destroy','description'=>'删除客户表单'],
                    ],
                ],
                [
                    'name' => 'report/*',
                    'description'=>'数据报告',
                    'child' => [
                        ['name'=>'report/campaign','description'=>'呼出任务统计'],
                        ['name'=>'report/extension','description'=>'分机统计'],
                        ['name'=>'report/extension-monitor','description'=>'分机监控'],
                        ['name'=>'report/chanspy','description'=>'分机监听'],
                    ],
                ],
                [
                    'name' => 'cdr/*',
                    'description'=>'CDR通话记录',
                    'child' => [
                        ['name'=>'cdr/index','description'=>'记录列表'],
                        ['name'=>'cdr/download','description'=>'下载录音'],
                        ['name'=>'cdr/play','description'=>'播放录音'],
                        ['name'=>'cdr/destroy','description'=>'删除记录'],
                    ],
                ],
                [
                    'name' => 'voice/*',
                    'description'=>'语音消息管理',
                    'child' => [
                        ['name'=>'voice/index','description'=>'语音消息列表'],
                        ['name'=>'voice/create','description'=>'发送语音消息'],
                        ['name'=>'voice/destroy','description'=>'删除语音消息'],
                    ],
                ],
                [
                    'name' => 'project/*',
                    'description'=>'CRM客户管理',
                    'child' => [
                        ['name'=>'project/index','description'=>'项目列表'],
                        ['name'=>'project/create','description'=>'添加项目'],
                        ['name'=>'project/edit','description'=>'编辑项目'],
                        ['name'=>'project/destroy','description'=>'删除项目'],
                        ['name'=>'project/show','description'=>'项目详情'],
                        ['name'=>'project/remark','description'=>'更新项目备注'],
                        ['name'=>'project/node','description'=>'更新项目节点'],
                        ['name'=>'project/count','description'=>'项目概要'],
                        ['name'=>'project/node-list','description'=>'项目节点进度'],
                        ['name'=>'project/remark-list','description'=>'项目备注进度'],
                        ['name'=>'project/delete-list','description'=>'回收站'],
                        ['name'=>'project/delete-return','description'=>'还原记录'],
                        ['name'=>'project.display_all','description'=>'查看所有记录'],
                        ['name'=>'project/call','description'=>'在线拨号'],
                        ['name'=>'project/import','description'=>'导入'],
                    ],
                ],
            ];

            foreach ($datas as $data){
                $permission = $auth->createPermission($data['name']);
                $permission->description = $data['description'];
                $auth->add($permission);
                //如果有子权限则添加子权限
                if (isset($data['child'])&&!empty($data['child'])){
                    foreach ($data['child'] as $child){
                        $permission_child = $auth->createPermission($child['name']);
                        $permission_child->description = $child['description'];
                        $auth->add($permission_child);
                        echo "填充权限{$child['name']}-{$child['description']}\n";
                        /*if ($auth->canAddChild($permission,$permission_child)){
                            $auth->addChild($permission,$permission_child);
                        }*/
                    }
                }
                echo "填充角色{$data['name']}-{$data['description']}\n";
            }
            echo "============权限填充【完成】============\n";
            echo "\n";

            echo "============节点填充【开始】============\n";
            $nodes = [
                [
                    'name'  => '项目启动',
                    'created_at' => time(),
                    'updated_at' => time(),
                ],
                [
                    'name'  => '做资料',
                    'created_at' => time(),
                    'updated_at' => time(),
                ],
                [
                    'name'  => '移交后台',
                    'created_at' => time(),
                    'updated_at' => time(),
                ],
                [
                    'name'  => '办理完结',
                    'created_at' => time(),
                    'updated_at' => time(),
                ]
            ];
            foreach ($nodes as $node){
                $connection->insert('node',$node)->execute();
                echo "添加节点".$node['name']."\n";
            }
            echo "============节点填充【完成】============\n";
            echo "\n";

            echo "============项目填充【开始】============\n";
            for ($n=0; $n<10; $n++){
                $project = [
                    'name'  => Person::firstNameMale(),
                    'sex' => mt_rand(1,3),
                    'phone' => PhoneNumber::numberBetween(1300000000,19999999999),
                    'company' => Company::companyPrefix().Company::companySuffix(),
                    'created_user_id' => 1,
                    'created_at' => time(),
                    'updated_at' => time(),
                ];
                $connection->insert('project',$project)->execute();
                echo "添加项目".json_encode($project)."\n";
            }
            echo "============项目填充【完成】============\n";
            echo "\n";

            $transaction->commit();

        }catch (\Exception $exception){
            $transaction->rollBack();
            echo "?????????????????????????????????????\n";
            echo "???????????????运行出错???????????????\n";
            echo "?????????????????????????????????????\n";
            echo $exception->getMessage();
            echo "\n";
        }

        return ExitCode::OK;
    }

}
